using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FiltronASP.Helpers.AjaxModel;
using System.Threading;
using System.Net;

namespace FiltronASP.Ajax
{
    public partial class truckFiltersTable : System.Web.UI.Page
    {
        private string result;

        protected void Page_Load(object sender, EventArgs e)
        {
            AddOnPreRenderCompleteAsync(new BeginEventHandler(Thread_BeginEvent), new EndEventHandler(Thread_EndEvent));
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }

        IAsyncResult Thread_BeginEvent(object sender, EventArgs e, AsyncCallback cb, object extraData)
        {
            WaitCallback w = new WaitCallback(Thread_DoWork);
            return w.BeginInvoke(sender, cb, w);
        }

        void Thread_EndEvent(IAsyncResult ar)
        {
            WaitCallback w = ar.AsyncState as WaitCallback;
            if (w != null) {
                w.EndInvoke(ar);
                Response.Clear();
                Response.Write(result);
                Context.ApplicationInstance.CompleteRequest();
            }   //if
        }

        void Thread_DoWork(object state)
        {
            this.result = String.Empty;
            try
            {
                if (Request["engineId"] != null)
                {
                    int engineId = int.Parse(Request["engineId"]);
                    int modelId = int.Parse(Request["modelId"]);
                    string languageName = Request["languageName"];
                    this.result = truckModelHelper.GetFiltersByEngineAndModel(engineId, modelId, languageName);
                }   //if
                else
                {
                }   //else
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch            
        }


    }
}
